package com.len.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;

/**
 * 
 * @ClassName: WebMvcConfig   
 * @Description: spring页面相关配置
 * @author: Kitty
 * @date: 2019年3月10日 下午12:59:26   
 *
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

	@Value("${lenosp.imagePath}")
	private String imagePath;
	@Value("${lenosp.uploadPath}")
	private String filePath;

	@Bean
	public HttpMessageConverter<String> responseBodyConverter() {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		return converter;
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
		converters.add(responseBodyConverter());
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
		registry.addResourceHandler("/plugin/**", "/static/**").addResourceLocations("classpath:/plugin/",
				"classpath:/static/");
		registry.addResourceHandler("/ftl/**").addResourceLocations("classpath:/ftl/");
		registry.addResourceHandler("/images/**").addResourceLocations("file:" + imagePath);
		registry.addResourceHandler("/img/**").addResourceLocations("file:" + imagePath);
		registry.addResourceHandler("/file/**").addResourceLocations("file:" + imagePath);
		super.addResourceHandlers(registry);
	}

	/* 保留国际化 */
	@Bean
	public LocaleChangeInterceptor interceptor() {
		LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
		interceptor.setParamName("lang");
		return interceptor;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(interceptor());
	}

	@Bean
	public LocaleResolver resolver() {
		SessionLocaleResolver resolver = new SessionLocaleResolver();
		resolver.setDefaultLocale(Locale.US);
		return resolver;
	}
}
